print("加载main.lua！")
PROJECT = "LS_WIFI"
VERSION = "0.1.3"
require "global"
require "sys"
require "misc"
require "net"
require "Uart_Task"
require "AliYun_Task"
require "fn"
require "wdt"
require "netLed"

print("主程序开始运行。。。33")

local function subCallBack(...)
    print("rev", arg[1])
end

local function pub(msg)
    print("pub")
    sys.publish("TEST", msg)
end

sys.subscribe("TEST", subCallBack)

local function ImeiGet()
    print("手机2G模块任务开始。。。")
    local imei = ""
    while string.len(imei) < 1 do
        sys.wait(200)
        imei = misc.getImei()
        --imei = "866289030123006"
        print("模块的IMEI是", imei)
        _G.MOD_IMEI = imei
    end
end

local function NetStatGet()
    print("查询网络状态。。。")
    while _G.MOD_NET_STAT < _G.STAT_GSM_OK do
        if net.getState() == "REGISTERED" then
            _G.MOD_NET_STAT = _G.STAT_GSM_OK
        end
        if net.getState() == "UNREGISTER" then
            _G.MOD_NET_STAT = _G.STAT_GSM_FAIL
        end
        sys.wait(1000)
    end
    print("停止查询网络状态。")
    print(_G.MOD_IMEI)
    print(_G.MOD_NET_STAT)
end
local function Module_PingPong()
    while true do
        if _G.SYS_BUSY == 0 then
            fn.uart_answer(0x00)
        end
        if _G.SYS_BUSY == 1 then
            print("Busy PingPong");
        end
        sys.wait(10000)
    end
end

-- netLed.setup(true,pio.P0_28)   --air800
netLed.setup(true,pio.P1_1)   --air202


sys.taskInit(ImeiGet)
sys.taskInit(NetStatGet)
sys.taskInit(Module_PingPong)

print("mem:",_G.collectgarbage("count"))  -- 打印占用的RAM
print("flash:",rtos.get_fs_free_size()) -- 打印剩余FALSH，单位Byte
print("env_use:",rtos.get_env_usage())
sys.init(0, 0)
sys.run()
